// 享元类的接口
abstract class Flyweight {
  abstract Operation(i: number): void
}

// 共享的具体享元类
class ConcreteFlyweight extends Flyweight {
  Operation(i: number): void {
    console.log(`具体的享元类 ${i}`)
  }
}

// 不共享的具体享元类
class UbsharedFlyweight extends Flyweight {
  Operation(i: number): void {
    console.log(`不共享的享元类对象： ${i}`)
  }
}

// 享元工厂类
class FlyweightFactory {
  private flyweights = new Map<string, ConcreteFlyweight>()

  constructor() {
    this.flyweights.set("A", new ConcreteFlyweight())
    this.flyweights.set("B", new ConcreteFlyweight())
    this.flyweights.set("C", new ConcreteFlyweight())
    this.flyweights.set("D", new ConcreteFlyweight())
  }

  GetFlywight(theKey: string): Flyweight {
    return this.flyweights.get(theKey) || new UbsharedFlyweight()
  }
}

(function () {
  let i = 100

  let flyweightFactory = new FlyweightFactory()

  let flyA = flyweightFactory.GetFlywight("A")
  flyA.Operation(i)

  let flyB = flyweightFactory.GetFlywight("B")
  flyB.Operation(i * 2)

  let flyC = flyweightFactory.GetFlywight("C")
  flyC.Operation(i * 3)

  let flyD = flyweightFactory.GetFlywight("D")
  flyD.Operation(i * 4)

  let flyE = new UbsharedFlyweight()
  flyE.Operation(i / 2)

  let flyF = flyweightFactory.GetFlywight("F")
  flyF.Operation(i / 2)

}())